home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gsview10.zip / display.c < prev    next >
C/C++ Source or Header  |  1993-08-05  |  18KB  |  696 lines

  1. /*
  2.  * display.c -- Ghostscript display operations for GSVIEW.EXE, 
  3.  *              a graphical interface for MS-Windows Ghostscript
  4.  * Copyright (C) 1993  Russell Lang
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *   Author: Russell Lang
  21.  * Internet: rjl@monu1.cc.monash.edu.au
  22.  */
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <windowsx.h>
  27. #include <commdlg.h>
  28. #include <shellapi.h>
  29. #include <mmsystem.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <dir.h>
  35. #include <io.h>
  36. #define NeedFunctionPrototypes 1
  37. #include "ps.h"
  38. #include "gsview.h"
  39.  
  40. struct ftime dftime;    /* time/date of selected file */
  41. long dflength;        /* length of selected file */
  42.  
  43. /* get current media index to papersizes[], or -1 if no match */
  44. int
  45. get_papersizes_index()
  46. {
  47. int i;
  48.     for (i=0; papersizes[i].name != (char *)NULL; i++) {
  49.         if (!stricmp(papersizes[i].name, medianame))
  50.         return i;
  51.     }
  52.     return -1;
  53. }
  54.  
  55. /* calculate bitmap size for gswin */
  56. void
  57. gswin_size()
  58. {
  59. int i = get_papersizes_index();
  60.     if ( (xdpi == 0.0) || (ydpi == 0.0) )
  61.         xdpi = ydpi = DEFAULT_RESOLUTION;
  62.     epsf_clipped = FALSE;
  63.     switch (orientation) {
  64.         case IDM_LANDSCAPE:
  65.         case IDM_SEASCAPE:
  66.         if (i < 0) {
  67.             bitmap_width = user_height;
  68.             bitmap_height = user_width;
  69.         }
  70.         else {
  71.             bitmap_width = papersizes[i].height;
  72.             bitmap_height = papersizes[i].width;
  73.         }
  74.         break;
  75.         default:
  76.         if ((doc != (struct document *)NULL) && doc->epsf
  77.             && epsf_clip) {
  78.             epsf_clipped = TRUE;
  79.             bitmap_width = doc->boundingbox[URX] - doc->boundingbox[LLX];
  80.             bitmap_height = doc->boundingbox[URY] - doc->boundingbox[LLY];
  81.         }
  82.         else if (i < 0) {
  83.             bitmap_width = user_width;
  84.             bitmap_height = user_height;
  85.         }
  86.         else {
  87.             bitmap_width = papersizes[i].width;
  88.             bitmap_height = papersizes[i].height;
  89.         }
  90.     }
  91.     bitmap_width  = (unsigned int)(bitmap_width  / 72.0 * xdpi);
  92.     bitmap_height = (unsigned int)(bitmap_height / 72.0 * ydpi);
  93. }
  94.  
  95. /* change the size of the gswin image if open */
  96. void
  97. gswin_resize()
  98. {
  99. BOOL display = FALSE;
  100. BOOL opened_dfile = FALSE;
  101.     gswin_size();
  102.     if (gswin_hinst == (HINSTANCE)NULL)
  103.         return;
  104.     if ( (dfile == (FILE *)NULL) && (doc != (struct document *)NULL) ) {
  105.         dfreopen();
  106.         opened_dfile = TRUE;
  107.     }
  108.     if (redisplay && page_ready && (doc != (struct document *)NULL))
  109.         display = TRUE;    /* redisplay page after resize */
  110.     gsview_endfile();
  111.     if (gswin_hinst != (HINSTANCE)NULL) {
  112.         fprintf(cfile,"mark /HWSize [%u %u]\r\n",bitmap_width,bitmap_height);
  113.         fprintf(cfile,"/HWResolution [%g %g]\r\n",xdpi,ydpi);
  114.         fprintf(cfile,"currentdevice putdeviceprops pop erasepage flushpage\r\n");
  115.         pipeflush();
  116.     }
  117.     if (display) {
  118.         if (gswin_hinst != (HINSTANCE)NULL)
  119.             gswin_open();    /* we need it open to redisplay */
  120.            fix_orientation(cfile);
  121.            dsc_header(cfile);
  122.         dsc_getpages(cfile,pagenum,pagenum);
  123.         pipeflush();
  124.     }
  125.  
  126.     if (opened_dfile)
  127.         dfclose();
  128. }
  129.  
  130. void
  131. gsview_orientation(int new_orientation)
  132. {
  133.     if (new_orientation == orientation)
  134.         return;
  135.     if (new_orientation == IDM_SWAPLANDSCAPE) {
  136.         swap_landscape = !swap_landscape;
  137.         if (swap_landscape) 
  138.             CheckMenuItem(hmenu, IDM_SWAPLANDSCAPE, MF_BYCOMMAND | MF_CHECKED);
  139.         else
  140.             CheckMenuItem(hmenu, IDM_SWAPLANDSCAPE, MF_BYCOMMAND | MF_UNCHECKED);
  141.         if ((orientation != IDM_LANDSCAPE) && (orientation != IDM_SEASCAPE))
  142.             return;
  143.     }
  144.     else {
  145.         CheckMenuItem(hmenu, orientation, MF_BYCOMMAND | MF_UNCHECKED);
  146.         orientation = new_orientation;
  147.         CheckMenuItem(hmenu, orientation, MF_BYCOMMAND | MF_CHECKED);
  148.     }
  149.     gswin_resize();
  150.     return;
  151. }
  152.  
  153. void
  154. gsview_media(int new_media)
  155. {
  156.     if ( (new_media == media) && (new_media != IDM_USERSIZE) )
  157.         return;
  158.     CheckMenuItem(hmenu, media, MF_BYCOMMAND | MF_UNCHECKED);
  159.     media = new_media;
  160.     CheckMenuItem(hmenu, media, MF_BYCOMMAND | MF_CHECKED);
  161.     GetMenuString(hmenu, media, medianame, sizeof(medianame), MF_BYCOMMAND);
  162.     gswin_resize();
  163.     return;
  164. }
  165.  
  166. /* run Ghostscript for previewing document */
  167. /* return TRUE if ok, FALSE if error */
  168. BOOL
  169. gswin_open()
  170. {
  171. char command[256];
  172.     /* return if already open */
  173.     if ((gswin_hinst != (HINSTANCE)NULL) && IsWindow(hwndimgchild))
  174.         return TRUE;
  175.  
  176.     pipeinit();        /* so we wait for first request */
  177.     gswin_size();
  178.     sprintf(command,"%s %s -r%gx%g -g%ux%u -sGSVIEW=%u -",
  179.         szGSwin, safer ? "-dSAFER" : "", xdpi, ydpi, 
  180.                 bitmap_width, bitmap_height, (unsigned int)hwndimg);
  181.     if (strlen(command) > 126) {
  182.         info_wait(FALSE);
  183.         gserror(IDS_TOOLONG, command, MB_ICONSTOP, SOUND_ERROR);
  184.         gswin_hinst = (HINSTANCE)NULL;
  185.         return FALSE;
  186.     }
  187.     gswin_hinst = (HINSTANCE)WinExec(command, SW_SHOWMINNOACTIVE);
  188.  
  189.     if (gswin_hinst < HINSTANCE_ERROR) {
  190.         info_wait(FALSE);
  191.         gserror(IDS_CANNOTRUN, command, MB_ICONSTOP, SOUND_ERROR);
  192.         gswin_hinst = (HINSTANCE)NULL;
  193.         return FALSE;
  194.     }
  195.     if (hwndtext == (HWND)NULL) {
  196.         /* we are running an incompatible version of Ghostscript */
  197.         hwndtext = FindWindow("BCEasyWin","Ghostscript");
  198.         if (hwndtext) {
  199.             SendMessage(hwndtext, WM_CHAR, 'q', 1L);
  200.             SendMessage(hwndtext, WM_CHAR, 'u', 1L);
  201.             SendMessage(hwndtext, WM_CHAR, 'i', 1L);
  202.             SendMessage(hwndtext, WM_CHAR, 't', 1L);
  203.             SendMessage(hwndtext, WM_CHAR, '\r', 1L);
  204.         }
  205.         hwndtext = (HWND)NULL;
  206.         hwndimgchild = (HWND)NULL;
  207.         gswin_hinst = (HINSTANCE)NULL;
  208.         clear_timer();
  209.         info_wait(FALSE);
  210.         gserror(IDS_WRONGGS, NULL, MB_ICONSTOP, SOUND_ERROR);
  211.         return FALSE;
  212.     }
  213.     saved = FALSE;
  214.  
  215.     /* wait for gswin to initialise */
  216.     if (set_timer(CLOSE_TIMEOUT))
  217.         EnableWindow(hwndimg, FALSE);
  218.     while (!is_pipe_done()&&  !bTimeout)
  219.         do_message();    /* wait for pipe data request from gswin */
  220.     clear_timer();
  221.     EnableWindow(hwndimg, TRUE);
  222.  
  223.     cfile = pipeopen();    /* open pipe to gswin */
  224.     BringWindowToTop(hwndimg);
  225.     SetFocus(hwndimg);    /* kludge: without this desktop gets focus */
  226.     return TRUE;
  227. }
  228.  
  229. /* close Ghostscript */
  230. BOOL
  231. gswin_close()
  232. {
  233. BOOL force = FALSE;
  234.     if (gswin_hinst == (HINSTANCE)NULL)
  235.         return TRUE;
  236.  
  237.     if (doc == (struct document*)NULL) {
  238.         /* we don't know how many pages remain so we must force an exit */
  239.         if (!is_pipe_done())
  240.         force = TRUE;
  241.     }
  242.     else {
  243.         if (page_ready)
  244.         next_page();
  245.     }
  246.  
  247.     if (!force) {
  248.         /* try to close Ghostscript cleanly */
  249.         pipeclose();
  250.         if (set_timer(CLOSE_TIMEOUT))
  251.         EnableWindow(hwndimg, FALSE);
  252.         while (GetModuleUsage(gswin_hinst) &&  !bTimeout)
  253.         do_message();    /* wait for gswin to close */
  254.         clear_timer();
  255.         EnableWindow(hwndimg, TRUE);
  256.     }
  257.     do_message();
  258.  
  259.     /* if still there try killing it a using a brute force method */
  260.     if (IsWindow(hwndtext)) {
  261.         if (is_win31) {
  262.             SendMessage(hwndtext, WM_CLOSE, 0, 0L);
  263.             if (IsWindow(hwndtext))
  264.             SendMessage(hwndtext, WM_CLOSE, 0, 0L);
  265.         }
  266.         else {
  267.             /* Windows 3.0 hangs if we use SendMessage */
  268.             PostMessage(hwndtext, WM_CLOSE, 0, 0L);
  269.             do_message();
  270.         }
  271.     }
  272.  
  273.     do_message();
  274.     gswin_hinst = (HINSTANCE)NULL;
  275.     hwndimgchild = (HWND)NULL;
  276.     hwndtext = (HWND)NULL;
  277.     bitmap_scrollx = bitmap_scrolly = 0;
  278.     page_ready = FALSE;
  279.     saved = FALSE;
  280.     pipeclose();
  281.     return TRUE;
  282. }
  283.  
  284. /* send a NEXT_PAGE message to Ghostscript */
  285. void
  286. next_page()
  287. {
  288. int i;
  289.     if (hwndimgchild && IsWindow(hwndimgchild)) {
  290.         SendMessage(hwndi